
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import DAO.DAO_DSDK;
import DBTransmit.TableDSDKRecord;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
import ticketbooking.Utils;

/**
 *
 * @author LEHOANGKHANH
 */
public class QLDSDangKi extends javax.swing.JInternalFrame {

    /**
     * Creates new form QLDSDangKi
     */
    SecretKey secretKey = null;
    public QLDSDangKi(SecretKey key) {
        initComponents();
        this.secretKey = key;
        InitTable_ComboBox();
    }
    public void InitTable_ComboBox(){
        List<TableDSDKRecord> lst = DAO_DSDK.LoadDS(secretKey);
        Utils.LoadDataToTable(jTable1, lst);
        List<String> lstMa = new ArrayList<String>();
        if(lst == null){
            return;
        }
        for(int i=0; i<lst.size(); i++){
            lstMa.add(lst.get(i).getMaCT());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        username = new javax.swing.JTextField();
        slve = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        sua = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        ok = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        id = new javax.swing.JTextField();
        chuyentau = new javax.swing.JComboBox();

        setClosable(true);
        setIconifiable(true);
        setTitle("Quản Lý Danh Sách Đặt Vé");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("UserName:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Chuyến Tàu:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 204));
        jLabel1.setText("Quản Lý Danh Sách Đăng Kí");

        username.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        slve.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Số Lượng Vé Đã Đặt:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, "sdf", null, null}
            },
            new String [] {
                "ID", "UserName", "Chuyến Tàu", "Số Lượng Vé Đã Đặt"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/add.png"))); // NOI18N
        jButton3.setText("Thêm");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        sua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/modify.png"))); // NOI18N
        sua.setText("Sửa");
        sua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                suaActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/erase.png"))); // NOI18N
        jButton1.setText("Xóa");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/1403367972_clean.png"))); // NOI18N
        ok.setEnabled(false);
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("ID:");

        id.setEditable(false);
        id.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        chuyentau.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        chuyentau.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(87, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addGap(18, 18, 18)
                        .addComponent(sua)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addGap(104, 104, 104))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(slve)
                            .addComponent(username)
                            .addComponent(id)
                            .addComponent(chuyentau, 0, 135, Short.MAX_VALUE))
                        .addGap(29, 29, 29)
                        .addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(76, 76, 76))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addComponent(ok))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(chuyentau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(slve, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(sua)
                    .addComponent(jButton1))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        id.setText("");
        username.setText("");
        slve.setText("");
        ok.setEnabled(true);
        sua.setEnabled(false);
        id.setEditable(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void suaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_suaActionPerformed
        // TODO add your handling code here:
        TableDSDKRecord record = new TableDSDKRecord();
        record.setID(id.getText());
        record.setMaCT(chuyentau.getSelectedItem().toString());
        record.setUserName(username.getText());
        record.setSLVe(Integer.parseInt(slve.getText().toString()));
        boolean state = DAO_DSDK.Update(record, secretKey);
        if(state == true){
            JOptionPane.showMessageDialog(rootPane, "Sửa dữ liệu thành công!");
            InitTable_ComboBox();
        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Sửa dữ liệu thất bại");
        }
    }//GEN-LAST:event_suaActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        boolean state = DAO_DSDK.Delete(id.getName(), secretKey);
        if(state == true){
            JOptionPane.showMessageDialog(rootPane, "Xóa dữ liệu thành công");
            InitTable_ComboBox();
        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Xóa dữ liệu thất bại!");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        // TODO add your handling code here:
        if(id.getText().equals("")){
            JOptionPane.showMessageDialog(rootPane, "Chưa nhập ID!");
            return;
        }
        TableDSDKRecord record = new TableDSDKRecord();
        record.setID(id.getText());
        record.setMaCT(chuyentau.getSelectedItem().toString());
        record.setUserName(username.getText());
        record.setSLVe(Integer.parseInt(slve.getText().toString()));
        boolean state = DAO_DSDK.Add(record, secretKey);
        if(state == true){
            JOptionPane.showMessageDialog(rootPane, "Thêm dữ liệu thành công!");
            InitTable_ComboBox();
            ok.setEnabled(false);
            sua.setEnabled(true);
            id.setEditable(false);
        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Thêm dữ liệu thất bại!");
            ok.setEnabled(false);
            sua.setEnabled(true);
            id.setEditable(false);
        }
    }//GEN-LAST:event_okActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int rowIndex = jTable1.getSelectedRow();
        if(rowIndex == -1){
            return;
        }
        id.setText(jTable1.getValueAt(rowIndex, 0).toString());
        username.setText(jTable1.getValueAt(rowIndex, 1).toString());
        chuyentau.setSelectedItem(jTable1.getValueAt(rowIndex, 2).toString());
        slve.setText(jTable1.getValueAt(rowIndex, 3).toString());
    }//GEN-LAST:event_jTable1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox chuyentau;
    private javax.swing.JTextField id;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton ok;
    private javax.swing.JTextField slve;
    private javax.swing.JButton sua;
    private javax.swing.JTextField username;
    // End of variables declaration//GEN-END:variables
}
